package applet.configurationManagement;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;


public class OptionsReader {

        private String _optionsFileName = null;
        
        private String _name = null;
        private String _address = null;
        private String _port = null;
        
        private String _ASAddress = null;
        private String _ASPort = null;
        
        private String _mediaName = null;
        private int _playerport = 0;
        private int _receivingport = 0;
        private int _sendinglocalport = 0;
        private String _vlcPath = null;
        
        private HashMap<String, String> _options = null;
        
        public OptionsReader (){
                _optionsFileName = new String ("Options.txt");
                _options = new HashMap<String, String>();
                readOptionsFile();
                processOptions();
        }
        
        public OptionsReader (String n){
                _optionsFileName = new String (n+ "Options.txt");
                _options = new HashMap<String, String>();
                readOptionsFile();
                processOptions();
        }
        
        private void processOptions() {
        		set_address(_options.get("address"));
        		set_port(_options.get("port"));
        		set_ASAddress(_options.get("asaddress"));
        		set_ASPort(_options.get("asport"));
        		set_mediaName(new String (_options.get("medianame")));
        		set_name(_options.get("name"));
        		set_playerport(Integer.parseInt(_options.get("playerport")));
        		set_vlcPath(_options.get("vlcpath"));
        		set_receivingport(Integer.parseInt(_options.get("receivingport")));
        		set_sendinglocalport(Integer.parseInt(_options.get("sendinglocalport")));
        }

		private void readOptionsFile() {
                
            FileInputStream fileIS = null;
            DataInputStream  dataIS = null;
            BufferedReader  bufR = null;
            
            File file = new File(_optionsFileName);

            try {
                fileIS = new FileInputStream(file);

                        // Here BufferedInputStream is added for fast reading.
                        dataIS = new DataInputStream(fileIS);
                        bufR = new BufferedReader(new InputStreamReader(dataIS));

                        // _dataIS.available() returns 0 if the file does not have more lines.
                        while (processElement(bufR.readLine()) != null) {
                        }
                        
                        // dispose all the resources after using them.
                        bufR.close();
                        dataIS.close();
                        fileIS.close();
                                
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

        private String processElement (String c){
                if (c != null){
                        c.trim();
                        int pos =-1;
                        if (!c.startsWith("//") && c.endsWith(";") && c.contains(":")){
                                pos = c.indexOf(':');
                                _options.put(new String(c.substring(0, pos)).trim().toLowerCase(), new String(c.substring(pos+1, c.length()-1)).trim());
                        }
                }
                return c;
        }

        public String get_optionsFileName() {
                return _optionsFileName;
        }

        public String get_address() {
                return _address;
        }

        public String get_ASAddress() {
                return _ASAddress;
        }

        public int get_port(){
        	if (_port == null)
        		return 5060;
    		else
    			return new Integer(_port).intValue();
        }
        
        public int get_ASPort(){
        	if (_ASPort == null)
        		return 5060;
    		else
    			return new Integer(_ASPort).intValue();
        }
        

		public void set_mediaName(String _mediaName) {
			this._mediaName = _mediaName;
		}

		public String get_mediaName() {
			return _mediaName;
		}

		public void set_name(String _name) {
			this._name = _name;
		}

		public String get_name() {
			return _name;
		}


        private void set_playerport(int port) {
			_playerport = port;
		}

		public int get_playerport() {
			return _playerport;
		}

		public void set_address(String _address) {
			this._address = _address;
		}

		public void set_port(String _port) {
			this._port = _port;
		}

		public void set_ASAddress(String address) {
			_ASAddress = address;
		}

		public void set_ASPort(String port) {
			_ASPort = port;
		}

		public void set_vlcPath(String string) {
			_vlcPath = string;
		}
		
		public String get_vlcPath(){
			return _vlcPath;
		}

		public int get_receivingport() {
			return _receivingport;
		}

		public void set_receivingport(int _receivingport) {
			this._receivingport = _receivingport;
		}

		public int get_sendinglocalport() {
			return _sendinglocalport;
		}

		public void set_sendinglocalport(int _sendinglocalport) {
			this._sendinglocalport = _sendinglocalport;
		}
        
}
